<?php

/**
 *
 *	@author Joshgeng <[<pengqiao.geng@zhaopin.com.cn>]>
 *	@date(2017.8.31) 创建
 *	@date(2017.8.31) 加入注册逻辑
 * 
 */
class Groupusers_model extends CI_Model{

	function __construct()
	{
		$this->load->helper('date');
		$this->load->database();
	}

	/**
	 * 获取所有的用户组列表
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	function groupusers_list($inputs)
	{
		$this->db->select('ZL_POWER_GROUP_ID');
		$zl_group_count = $this->db->get('zl_power_group')->num_rows();

		$this->db->select('ZL_POWER_GROUP_ID group_id,ZL_POWER_GROUP_TIME time,ZL_POWER_GROUP_NAME name');
		$this->db->limit($inputs['limit'],$inputs['offset']);
		$zl_group = $this->db->get('zl_power_group');

		return array(
			'status' => 1,
			'data' => $zl_group->result_array(),
			'recordsTotal' => $zl_group_count,
			'recordsFiltered' => $zl_group->num_rows()
		);
	}
		
	/**
	 * 读取指定用户组信息
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 * @author xiaoting 
	 */
	function groupusers_read($inputs)
	{
		$this->db->where('ZL_POWER_GROUP_ID',$inputs['id']);
		$this->db->select('ZL_POWER_GROUP_TIME time,ZL_POWER_GROUP_NAME name');

		$groupuser_data = $this->db->get('zl_power_group');
		//$groupuser_array = $groupuser_data->result_array();
		//$id = $groupuser_array['ZL_POWER_GROUP_ID'];
		if ($groupuser_data->num_rows() < 1){
			return array('status' => 0);
		}

		$this->db->select('ZL_POWER_ID id,ZL_POWER_NAME power_name,ZL_POWER_POWERID power_id');
		$this->db->join('zl_power_group_list', 'zl_power_group_list.ZL_POWER_GROUP_LIST_POWERID = zl_power.ZL_POWER_ID','left');
		$this->db->where('ZL_POWER_GROUP_LIST_GROUPID',$inputs['id']);
		//$this->db->limit($inputs['limit'],$inputs['offset']);
		$power_data = $this->db->get('zl_power');

		return array('status' => 1, 'data' => array_merge($groupuser_data->result_array(),$power_data->result_array()));
	}

	/**
	 * 列出所有权限信息
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	function groupusers_addlist($inputs)
	{
		$this->db->select('ZL_POWER_ID id,ZL_POWER_NAME name,ZL_POWER_POWERID powerid');
		$this->db->order_by('ZL_POWER_POWERID','ASC');
		$zl_power = $this->db->get('zl_power');
		if ($zl_power->num_rows()<1) {
			return array(
					'status' => 0
			);
		}
		return array(
			'status' => 1,
			'data' => $zl_power->result_array()
		);
	}

	/**
	 * 添加用户组信息
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	function groupusers_add($inputs)
	{
		$data = array(
			'ZL_POWER_GROUP_NAME' => $inputs['name'],
			'ZL_POWER_GROUP_TIME' => $this->comon->_datetime()
		);

		$return_group = $this->db->insert('zl_power_group',$data);
		$groupid =  $this->db->insert_id();
		if (!$return_group){
			return array('status' => 0);
		}
		$powerid = explode(',', $inputs['powerid']);
		$data_list = array();
		$return_group = '';
		if (count($powerid) > 1) {
			foreach ($powerid as $key => $value) {
				if ($value!='') {
					array_push($data_list,
						array('ZL_POWER_GROUP_LIST_POWERID' => $value, 
							'ZL_POWER_GROUP_LIST_GROUPID' => $groupid
						)
					);
				}
			}
			$return_group =  $this->db->insert_batch('zl_power_group_list',$data_list);
		}
		else{
			$data_list = array(
				'ZL_POWER_GROUP_LIST_POWERID' => $inputs['powerid'],
				'ZL_POWER_GROUP_LIST_GROUPID' => $groupid
			);
			$return_group = $this->db->insert('zl_power_group_list',$data_list);
		}

		if (!$return_group){
			return array('status' => 0);
		}
		else{
			return array('status' => 1);
		}
	}

	/**
	 * 修改指定用户组信息
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 * @author xiaoting 
	 */
	function groupusers_change($inputs)
	{
		$this->db->where('ZL_POWER_GROUP_ID',$inputs['id']);
		$this->db->select('ZL_POWER_GROUP_ID group_id,ZL_POWER_GROUP_TIME time,ZL_POWER_GROUP_NAME name');
		$groupuser_data = $this->db->get('zl_power_group');

		$group_count = $this->db->count_all('zl_power');
		if ($groupuser_data->num_rows() < 1){
			return array('status' => 0);
		}

		$this->db->select('ZL_POWER_ID id,ZL_POWER_NAME name,ZL_POWER_POWERID powerid');
		$this->db->order_by('ZL_POWER_POWERID','ASC');
		$zl_power_all = $this->db->get('zl_power');

		$this->db->where('ZL_POWER_GROUP_LIST_GROUPID ',$inputs['id']);
		$this->db->select('ZL_POWER_GROUP_LIST_POWERID list_power_id');
		$zl_power_data = $this->db->get('zl_power_group_list');

		return array('status' => 1, 'data' => array_merge($groupuser_data->result_array(),$zl_power_all->result_array(),$zl_power_data->result_array()),'group_count' => $group_count);
	}

	/**
	 * 确认修改指定用户组信息
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 * @author xiaoting 
	 */
	function groupusers_surechange($inputs)
	{
		$data = array(
			'ZL_POWER_GROUP_TIME' => $this->comon->_datetime()
		);
		$this->db->where('ZL_POWER_GROUP_ID', $inputs['id']);
		$return_group = $this->db->update('zl_power_group',$data);
		if (!$return_group){
			return array('status' => 0);
		}

		$return_group = $this->db->delete('zl_power_group_list', array('ZL_POWER_GROUP_LIST_GROUPID' => $inputs['id']));
		if (!$return_group){
			return array('status' => 0);
		}

		$powerid = explode(',', $inputs['powerid']);
		$data_list = array();
		foreach ($powerid as $key => $value) {
			if ($value!='') {
				array_push($data_list,
					array('ZL_POWER_GROUP_LIST_POWERID' => $value,
						'ZL_POWER_GROUP_LIST_GROUPID' =>  $inputs['id']
					)
				);
			}
		}
		$return_group =  $this->db->insert_batch('zl_power_group_list',$data_list);
		if (!$return_group){
			return array('status' => 0);
		}
		else{
			return array('status' => 1);
		}
	}

	/**
	 * 删除用户组信息
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 * @author xiaoting 
	 */
	function groupusers_delete($inputs)
	{
		// 查找是否存在
		$this->db->where('ZL_POWER_GROUP_ID',$inputs['id']);
		$this->db->select('ZL_POWER_GROUP_ID');
		$zl_power_group_count = $this->db->get('zl_power_group')->num_rows();

		if ($zl_power_group_count < 1){
			return array('status' => 0);
		}

		$this->db->where('ZL_POWER_GROUP_ID',$inputs['id']);
		$delete = $this->db->delete('zl_power_group');

		if (!$delete){
			return array('status' => -1);
		}

		$this->db->where('ZL_POWER_GROUP_LIST_GROUPID',$inputs['id']);
		$delete = $this->db->delete('zl_power_group_list');

		if (!$delete){
			return array('status' => -2);
		}

		return array('status' => 1);

	}
	

	/**
	 * 添加用户
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	// function power_add($inputs)
	// {
	// 	$power_data = array(
	// 		'ZL_POWER_NAME' => $inputs['power_name'],
	// 		'ZL_POWER_ITMETIME' => $this->comon->_datetime(),
	// 		'ZL_POWER_POWERID' => $inputs['power_number']
	// 	);

	// 	$power_public = $this->db->insert('zl_power',$power_data);

	// 	if (!$power_public){
	// 		return array('status' => 0);
	// 	}
	// 	return array('status' => 1);
	// }
	
}

?>
